﻿/*******************************************************
 * 
 * 作者：胡庆访
 * 创建时间：20100330
 * 说明：所有实体集合类的基类
 * 运行环境：.NET 3.5 SP1
 * 版本号：1.0.0
 * 
 * 历史记录：
 * 创建文件 胡庆访 20100330
 * 添加EntityTreeList和EntityList，并实现ReadFromTable方法 胡庆访 20100402
 * 
*******************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using System.Diagnostics;
using OpenExpressApp.MetaModel;
using OpenExpressApp;

namespace OpenExpressApp
{
    /// <summary>
    /// 所有实体集合类的基类
    /// </summary>
    /// <typeparam name="TCollection"></typeparam>
    /// <typeparam name="TEntity"></typeparam>
    [Serializable]
    public abstract class EntityList<TCollection, TEntity> : GBusinessListBase<TCollection, TEntity>
        where TCollection : EntityList<TCollection, TEntity>
        where TEntity : Entity<TEntity>
    {
        protected EntityList()
        {
            this.AllowNew = true;
        }

        /// <summary>
        /// 此集合是不是合为孩子集合？
        /// </summary>
        protected virtual bool AsChild
        {
            get
            {
                return true;
            }
        }

        protected override object AddNewCore()
        {
            var item = this.AsChild ? Entity<TEntity>.NewChild() : Entity<TEntity>.New();
            this.Add(item);
            return item;
        }

        private void Child_Fetch() { }

        /// <summary>
        /// 获取一个空的TCollection，准备加入数据库中存储的数据
        /// </summary>
        /// <returns></returns>
        public static TCollection GetChild()
        {
            return DataPortal.FetchChild<TCollection>();
        }

        /// <summary>
        /// 创建一个新的集合
        /// </summary>
        /// <returns></returns>
        public static TCollection NewChild()
        {
            return DataPortal.CreateChild<TCollection>();
        }
    }

    /// <summary>
    /// 所有树型实体集合类的基类
    /// 
    /// 代码与TEntityList大致相同。。
    /// </summary>
    /// <typeparam name="TCollection"></typeparam>
    /// <typeparam name="TEntity"></typeparam>
    [Serializable]
    public abstract class EntityTreeList<TCollection, TEntity> : GBusinessOrderedTreeListBase<TCollection, TEntity>
        where TCollection : EntityTreeList<TCollection, TEntity>
        where TEntity : Entity<TEntity>, ITreeNode, IOrderedObject
    {
        protected EntityTreeList()
        {
            this.AllowNew = true;
        }

        /// <summary>
        /// 此集合是不是合为孩子集合？
        /// </summary>
        protected virtual bool AsChild
        {
            get
            {
                return true;
            }
        }

        protected override object AddNewCore()
        {
            var item = this.AsChild ? Entity<TEntity>.NewChild() : Entity<TEntity>.New();
            this.Add(item);
            return item;
        }

        private void Child_Fetch() { }

        public static TCollection GetChild()
        {
            return DataPortal.FetchChild<TCollection>();
        }

        protected override void Child_Update(params object[] parameters)
        {
            var oldRLCE = this.RaiseListChangedEvents;
            this.RaiseListChangedEvents = false;
            try
            {
                foreach (var child in this)
                    DataPortal.UpdateChild(child, parameters);

                foreach (var child in DeletedList)
                    DataPortal.UpdateChild(child, parameters);
                DeletedList.Clear();
            }
            finally
            {
                this.RaiseListChangedEvents = oldRLCE;
            }
        }

        /// <summary>
        /// 约定：添加细表记录放放名为NewChild
        /// </summary>
        /// <returns></returns>
        public static TCollection NewChild()
        {
            return DataPortal.CreateChild<TCollection>();
        }
    }
}